package com.cosc.apgs.param.controller;

import com.cosc.apgs.param.data.UserInfoDO;
import com.cosc.apgs.param.data.UserInfoVO;
import com.cosc.apgs.param.service.LoginService;
import com.cosc.apgs.param.token.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 登录控制器
 * @createTime 2020年11月09日 11:27:00
 */

@Controller
@RequestMapping("/api/loginController/")
//Api注解用于swagger描述类
@Api(value = "LoginController")
public class LoginController {

    @Autowired
    private LoginService loginService;

    //http://localhost:8080/swagger-ui.html#/可以查看swagger页面。
    @GetMapping("/login")
    //ApiOperation注解用于swagger描述方法
    @ApiOperation(value = "登录系统", tags = {"登录系统"}, notes = "登录系统")
    @ResponseBody
    public UserInfoVO login(@ApiParam(name = "account", value = "账户", required = true) String account, @ApiParam(name = "password", value = "密码", required = true) String password) {

        UserInfoVO userInfoVO = new UserInfoVO();
        UserInfoDO userInfoDO = loginService.getUserInfo(account, password);

        if (userInfoDO == null) {
            userInfoVO.setSuccessFlag(false);
            userInfoVO.setErrorMessage("账户或者密码错误，请确认后重试!");
        }

        //对登录进行简单判断,符合逻辑代表用户名，密码输入正确
        if (userInfoDO != null) {
            String uuid = userInfoDO.getUuid();
            String token = TokenUtil.createToken(uuid);
            String userName = userInfoDO.getUserName();
            userInfoVO.setSuccessFlag(true);
            userInfoVO.setUuid(uuid);
            userInfoVO.setUserName(userName);
            userInfoVO.setToken(token);
        }

        return userInfoVO;
    }


//    /**
//     * 该接口需要带签名才能访问
//     *
//     * @return
//     */
//    @TokenCheck
//    @GetMapping("/getMessage")
//    public String getMessage() {
//        return "你已通过验证";
//    }

}
